package pers.agony.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import pers.agony.dao.UserMapper;
import pers.agony.pojo.User;
import pers.agony.service.UserService;

import java.util.List;

/**
 * @author : Agony
 * @date : Created in 9:51 下午 2021/11/6
 * @Description:
 * @Modified By:
 * @version: : 1.0
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public List<User> findAllUser() {
        //获取redis中的数据
        List<User> list = (List<User>) redisTemplate.boundValueOps("key_all").get();
        //判断是否有，如果有则返回，如果没有则从mysql中获取设置到redis中再返回
        if(list != null && list.size() > 0){
            return list;
        }
        //从mysql中获取设置到redis中再返回
        List<User> allUser = userMapper.findAllUser();
        redisTemplate.boundValueOps("key_all").set(allUser);
        return allUser;
    }

//    @Override
//    public List<User> findAllUser() {
//        return userMapper.findAllUser();
//    }

}
